home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / snip9611.zip / BASCNVRT.C < prev    next >
C/C++ Source or Header  |  1996-11-24  |  1KB  |  56 lines

  1. /* +++Date last modified: 02-Nov-1995 */
  2.  
  3. /*
  4. **  BASCNVRT.C - Convert between number bases
  5. **
  6. **  public domain demo by Bob Stout
  7. */
  8.  
  9. #include <stdlib.h>
  10. #include "extkword.h"
  11. #include "numcnvrt.h"
  12.  
  13. /*
  14. **  Calling parameters: 1 - Number string to be converted
  15. **                      2 - Buffer for the  converted output
  16. **                      3 - Radix (base) of the input
  17. **                      4 - Radix of the output
  18. **
  19. **  Returns: Pointer to converted output
  20. **
  21. **  Uses LTOSTR.C from SNIPPETS
  22. */
  23.  
  24. char *base_convert(const char *in, char *out, size_t len, int rin, int rout)
  25. {
  26.       long n;
  27.       char *dummy;
  28.  
  29.       n = strtol(in, &dummy, rin);
  30.       return ltostr(n, out, len, rout);
  31. }
  32.  
  33. #ifdef TEST
  34.  
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37.  
  38. int main(int argc, char *argv[])
  39. {
  40.       int rin, rout;
  41.       char buf[40];
  42.  
  43.       if (4 > argc)
  44.       {
  45.             puts("Usage: BASCNVRT <number> <base_in> <base_out>");
  46.             return(-1);
  47.       }
  48.       rin  = atoi(argv[2]);
  49.       rout = atoi(argv[3]);
  50.       printf("%s (base %d) = %s (base %d)\n", argv[1], rin,
  51.             base_convert((const char *)argv[1], buf, 40, rin, rout), rout);
  52.       return 0;
  53. }
  54.  
  55. #endif /* TEST */
  56.